<template>
  <div class="photo-grid">
    <div
      v-for="(photo, index) in photos"
      :key="photo.id"
      class="photo-item"
      @click="$emit('preview', index)"
    >
      <div class="photo-wrapper">
        <img
          :src="photo.url"
          :alt="photo.title || '照片'"
          loading="lazy"
          :data-index="index"
          class="gallery-img"
        />
        <div class="photo-overlay">
          <div class="photo-actions">
            <button class="action-btn preview" @click.stop="$emit('preview', index)">
              <i class="ri-eye-line"></i>
            </button>
            <button class="action-btn download desktop-only" @click.stop="$emit('download', photo)">
              <i class="ri-download-line"></i>
            </button>
          </div>
        </div>
      </div>
      <div v-if="photo.title" class="photo-title">{{ photo.title }}</div>
    </div>
  </div>
</template>

<script setup>
defineProps({
  photos: {
    type: Array,
    required: true,
  },
})

defineEmits(['preview', 'download'])

defineOptions({ name: 'PhotoGrid' })
</script>

<style lang="scss" scoped>
.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;

  .photo-item {
    .photo-wrapper {
      aspect-ratio: 1 / 1;
      position: relative;
      overflow: hidden;
      border-radius: 0.5rem;
      transition:
        transform 0.3s ease,
        box-shadow 0.3s ease;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
        transition: transform 0.5s ease;
      }

      .photo-overlay {
        position: absolute;
        inset: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 0, 0, 0.4);
        opacity: 0;
        transition: all 0.3s ease;

        .photo-actions {
          display: flex;
          gap: 0.75rem;

          .action-btn {
            width: 40px;
            height: 40px;
            background: white;
            border: none;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.2s ease;

            i {
              font-size: 1.25rem;
              color: var(--text-color);
            }

            &:hover {
              transform: scale(1.1);
            }

            &.preview:hover i {
              color: var(--primary-color);
            }

            &.download:hover i {
              color: #3b82f6;
            }
          }
        }
      }

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);

        .photo-overlay {
          opacity: 1;
        }

        img {
          transform: scale(1.05);
        }
      }
    }

    .photo-title {
      padding: 0.75rem 0;
      font-size: 0.9375rem;
      color: var(--text-color);
      text-align: center;
    }
  }
}

// 响应式调整
@media (max-width: 768px) {
  .photo-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 0.75rem;

    .photo-item {
      .photo-title {
        font-size: 0.875rem;
      }
    }
  }

  // 移动端隐藏下载按钮
  .desktop-only {
    display: none !important;
  }
}
</style>
